<template>
  <CommonPage>
    <template #action>
      <NButton v-permission="'AddTeam'" type="primary" @click="handleAdd()">
        <i class="i-material-symbols:add mr-4 text-18" />
        新增球队
      </NButton>
    </template>

    <MeCrud
      ref="$table"
      v-model:query-items="queryItems"
      :scroll-x="1200"
      :columns="columns"
      :get-data="api.read"
    >
      <MeQueryItem label="球队名称" :label-width="66">
        <n-input
          v-model:value="queryItems.team_name"
          type="text"
          placeholder="请输入球队名称"
          clearable
        />
      </MeQueryItem>

      <!-- <MeQueryItem label="赛季" :label-width="50">
        <n-select v-model:value="queryItems.session" clearable :options="sessionList" />
      </MeQueryItem> -->
    </MeCrud>

    <MeModal ref="modalRef" width="520px">
      <n-form
        ref="modalFormRef"
        label-placement="left"
        label-align="left"
        :label-width="80"
        :model="modalForm"
        :disabled="modalAction === 'view'"
      >
        <n-form-item
          label="球队名称"
          path="team_name"
          :rule="{
            required: true,
            message: '请输入球队名称',
            trigger: ['input', 'blur'],
          }"
        >
          <n-input v-model:value="modalForm.team_name" :disabled="modalAction !== 'add'" />
        </n-form-item>

        <n-form-item v-if="['add', 'edit'].includes(modalAction)" label="所属赛季" path="session">
          <n-select
            v-model:value="modalForm.session"
            :options="_sessionList"
            label-field="label"
            value-field="value"
            clearable
            filterable
          />
        </n-form-item>

        <n-form-item v-if="['add', 'edit'].includes(modalAction)" label="所属分组" path="group">
          <n-select
            v-model:value="modalForm.group"
            :options="_groupList"
            label-field="label"
            value-field="value"
            clearable
            filterable
          />
        </n-form-item>
      </n-form>
    </MeModal>
  </CommonPage>
</template>

<script setup>
import { NAvatar, NButton, NSwitch, NTag } from 'naive-ui'
import api from './api'
import { formatDateTime } from '@/utils'
import { MeCrud, MeModal, MeQueryItem } from '@/components'
import { useCrud } from '@/composables'

defineOptions({ name: 'TeamMgt' })

const $table = ref(null)
/** QueryBar筛选参数（可选） */
const queryItems = ref({
  session: '',
})

onMounted(() => {
  $table.value?.handleSearch()
})

const groupList = [
  { label: '全部', value: '' },
  { label: 'A组', value: 'A' },
  { label: 'B组', value: 'B' },
]
const _groupList = groupList.filter(item => item.value !== '')
const sessionList = [
  { label: '全部', value: '' },
  { label: '2025（第七届）', value: 7 },
  { label: '2024（第六届）', value: 6 },
]
const _sessionList = sessionList.filter(item => item.value !== '')
// const roles = ref([])
// api.getAllRoles().then(({ data = [] }) => (roles.value = data))

const {
  modalRef,
  modalFormRef,
  modalForm,
  modalAction,
  handleAdd,
  handleDelete,
  handleEdit,
} = useCrud({
  name: '球队',
  initForm: { enable: true, session: 7, group: 'A' },
  doCreate: api.create,
  doDelete: api.delete,
  doUpdate: api.update,
  refresh: () => $table.value?.handleSearch(),
})

const columns = [
  {
    title: '合照',
    key: 'avatar',
    width: 50,
    render: ({ avatar }) =>
      h(NAvatar, {
        size: 'medium',
        src: avatar,
      }),
  },
  { title: '队伍名称', key: 'team_name', width: 150, ellipsis: { tooltip: true } },
  { title: '所属小组', key: 'group', width: 80, ellipsis: { tooltip: true } },
  { title: '胜场', key: 'win', width: 80 },
  { title: '负场', key: 'fail', width: 80, ellipsis: { tooltip: true } },
  { title: '积分', key: 'point', width: 80, ellipsis: { tooltip: true } },
  { title: '净胜分', key: 'netwin', width: 80, ellipsis: { tooltip: true } },
  {
    title: '赛季',
    key: 'session',
    width: 80,
    ellipsis: { tooltip: true,
    // render(row) {
    //   return h('span', () => {

    //   })
    // }
    },
  },
  // {
  //   title: '创建时间',
  //   key: 'createTime',
  //   width: 100,
  //    render(row) {
  //     return h('span', formatDateTime(row.createTime))
  //   }
  // },
  {
    title: '操作',
    key: 'actions',
    width: 140,
    align: 'right',
    fixed: 'right',
    hideInExcel: true,
    render(row) {
      return [
        h(
          NButton,
          {
            size: 'small',
            type: 'primary',
            secondary: true,
            onClick: () => handleEdit(row),
          },
          {
            default: () => '编辑',
            icon: () => h('i', { class: 'i-carbon:user-role text-14' }),
          },
        ),

        h(
          NButton,
          {
            size: 'small',
            type: 'error',
            style: 'margin-left: 12px;',
            onClick: () => handleDelete(row.id),
          },
          {
            default: () => '删除',
            icon: () => h('i', { class: 'i-material-symbols:delete-outline text-14' }),
          },
        ),
      ]
    },
  },
]
</script>
